﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MusicPageViewSorter.cs" company="3po.ru">
//   Author: Alexander Ulitin [support@3po.ru]
//   
//   This file is part of Conusic.
//   
//   Conusic is free software: you can redistribute it and/or modify
//   it under the terms of the GNU General Public License as published by
//   the Free Software Foundation, either version 3 of the License, or
//   (at your option) any later version.
//   
//   Conusic is distributed in the hope that it will be useful,
//   but WITHOUT ANY WARRANTY; without even the implied warranty of
//   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   GNU General Public License for more details.
//   
//   You should have received a copy of the GNU General Public License
//   along with Conusic.  If not, see http://www.gnu.org/licenses/.
// </copyright>
// <summary>
//   The music page view sorter.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MusicPage
{
    using System.Collections;
    using System.Windows.Forms;

    /// <summary>
    /// The music page view sorter.
    /// </summary>
    public class MusicPageViewSorter : IComparer
    {
        #region Fields

        /// <summary>
        /// The column index.
        /// </summary>
        private int columnIndex;

        /// <summary>
        /// The reverse.
        /// </summary>
        private bool reverse = false;

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="MusicPageViewSorter"/> class.
        /// </summary>
        public MusicPageViewSorter()
        {
            this.columnIndex = 0;
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The compare.
        /// </summary>
        /// <param name="x">
        /// The x.
        /// </param>
        /// <param name="y">
        /// The y.
        /// </param>
        /// <returns>
        /// The <see cref="int"/>.
        /// </returns>
        public int Compare(object x, object y)
        {
            var lvi1 = x as ListViewItem;
            var lvi2 = y as ListViewItem;

            if (lvi1 != null && lvi2 != null && lvi1.SubItems.Count > this.columnIndex
                && lvi2.SubItems.Count > this.columnIndex)
            {
                int result = lvi1.SubItems[this.columnIndex].Text.CompareTo(lvi2.SubItems[this.columnIndex].Text);
                if (this.reverse)
                {
                    result = -result;
                }

                return result;
            }
            else
            {
                return 0;
            }
        }

        /// <summary>
        /// The set column sorting.
        /// </summary>
        /// <param name="column">
        /// The column.
        /// </param>
        public void SetColumnSorting(int column)
        {
            if (this.columnIndex == column)
            {
                this.reverse = !this.reverse;
            }

            this.columnIndex = column;
        }

        #endregion
    }
}